/*
 * @FastQueue.java	2008/07/28
 *
 * Copyright 2008 Web & Zen Multimedia, All rights reserved.
 * Use is subject to license terms.
 */
package it.wzm.common.lightweightlog.client;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Iterator;
import java.util.Collections;
import java.util.Properties;

/**
 * @author Leonardo Celati
 * @param <T> T extends Entry
 */
final class FastQueue<T extends Entry> extends Queue<T> implements Cumulable {

    // the Entry container for this Queue
    private List<T> l;

    /**
     * Default constructor 
     */
    public FastQueue(Properties p) {
        super(p);
        l = new ArrayList<T>();
    }

    /**
     * See if this cell is empty
     * @return true if it's empty, false if not
     */
    public boolean isEmpty() {
        return l.isEmpty();
    }

    /**
     * Store the element into the Queue
     * @param T
     * @return true if the element is correctly added to queue
     */
    public boolean push(T t) {
        return l.add(t);
    }

    /**
     * 
     * @return a sorted list containing all entry
     */
    @SuppressWarnings("unchecked")
    public LinkedList<T> retainAll() {
        // our entry will goes in there
        LinkedList<T> ll = new LinkedList<T>();
        // sort T
        Collections.sort(this.l);
        // time to iterate trough the container
        Iterator<T> i = this.l.iterator();
        // each element in container is added to the list
        while (i.hasNext()) {
            ll.add(i.next());
        }
        // cleaning the old queue
        this.l.clear();
        // returning the new one
        return ll;
    }

    /**
     * Retrieve the actual size of this queue
     * @return the size of this cell
     */
    public int size() {
        return l.size();
    }
}
